package com.lvtulife.common.component.log;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.sql.Date;

/**
 * 使用spring jdbc 操作数据库，进行日志的保存
 */
@Component(value = "SysLogJdbc")
public class SysLogJdbc {

    @Resource(name = "jdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    public void saveLog(final BaseLog log) {
        final String sql = "INSERT INTO base_log (log_type, method_type, clazz, method, params, description, ex_code, ex_detail, request_ip, created_id, created_dt) " +
                " VALUES(?,?,?,?,?,?,?,?,?,?,?)";

        this.jdbcTemplate.update(sql, ps -> {
            ps.setByte(1, log.getLogType());
            ps.setByte(2, log.getMethodType());
            ps.setString(3, log.getClazz());
            ps.setString(4, log.getMethod());
            ps.setString(5, log.getParams());
            ps.setString(6, log.getDescription());
            ps.setString(7, log.getExCode());
            ps.setString(8, log.getExDetail());
            ps.setString(9, log.getRequestIp());
            ps.setInt(10, log.getCreatedId());
            ps.setDate(11, new Date(new java.util.Date().getTime()));
        });
    }
}
